/*action create by pionner code builder*/
package com.gotop.sms.fysms;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Transaction;

import com.opensymphony.xwork2.Action;

public class BacterinAction implements Action, Serializable {

	// key
	private java.lang.Integer id;

	private java.lang.Integer[] ids;
	private Bacterin bacterin;
	private java.lang.String name;
	private java.lang.Float price;
	private java.lang.Integer offNum;
	private java.lang.String offNumUnit;
	private java.lang.String type;
	private java.lang.Integer targetBacterin;
	private java.lang.String targetBacterinName;
	private List objects=null;
	private String condition=null;
	private String fromTree=null;
	private Integer status;
	
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	/** 默认构造方法 */
	public BacterinAction(){
	}
	/** full constructor */
	public BacterinAction(java.lang.String name,java.lang.Float price,java.lang.Integer offNum,java.lang.String offNumUnit,java.lang.String type,java.lang.Integer targetBacterin,java.lang.String targetBacterinName){
		this.name=name;
		this.price=price;
		this.offNum=offNum;
		this.offNumUnit=offNumUnit;
		this.type=type;
		this.targetBacterin=targetBacterin;
		this.targetBacterinName=targetBacterinName;
	}
	public void setId(java.lang.Integer id){
		this.id=id;
	}
	public java.lang.Integer getId(){
		return id;
	}

	public void setIds(java.lang.Integer[] ids){
		this.ids=ids;
	}
	public java.lang.Integer[] getIds(){
		return ids;
	}

	public void setBacterin(Bacterin bacterin){
		this.bacterin=bacterin;
	}
	public Bacterin getBacterin(){
		return bacterin;
	}

	public void setName(java.lang.String name){
		this.name=name;
	}
	public java.lang.String getName(){
		return name;
	}

	public void setPrice(java.lang.Float price){
		this.price=price;
	}
	public java.lang.Float getPrice(){
		return price;
	}

	public void setOffNum(java.lang.Integer offNum){
		this.offNum=offNum;
	}
	public java.lang.Integer getOffNum(){
		return offNum;
	}

	public void setOffNumUnit(java.lang.String offNumUnit){
		this.offNumUnit=offNumUnit;
	}
	public java.lang.String getOffNumUnit(){
		return offNumUnit;
	}

	public void setType(java.lang.String type){
		this.type=type;
	}
	public java.lang.String getType(){
		return type;
	}

	public void setTargetBacterin(java.lang.Integer targetBacterin){
		this.targetBacterin=targetBacterin;
	}
	public java.lang.Integer getTargetBacterin(){
		return targetBacterin;
	}

	public List getObjects() {
		return objects;
	}

	public void setObjects(List objects) {
		this.objects = objects;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public String execute() throws Exception {
		return "SUCESS";
	}
	public String doTargetList(){
		BacterinDAO dao=new BacterinDAO();
		condition="status <> 0";
		this.objects=dao.findByCondition(condition);
		Bacterin birthday=new Bacterin();
		birthday.setName("出生日期");
		birthday.setId(new Integer("-1"));
		this.objects.add(0, birthday);
		return "list";
	}
	public String doList(){
		BacterinDAO dao=new BacterinDAO();
		
		condition="status <> 0";
		this.objects=dao.findByCondition(condition);
		
		for(int i=0;i<objects.size();i++){
			Bacterin obj=(Bacterin)objects.get(i);
			if(obj.getTargetBacterin()!=-1){
				Bacterin temp=dao.findById(obj.getTargetBacterin());
				obj.setTargetBacterinName(temp.getName());
			}else{
				obj.setTargetBacterinName("出生日期");				
			}
			objects.set(i, obj);			
		}
		return "list";
	}

	public String doAdd(){
		return "add";
	}

	public String doEdit(){
		BacterinDAO dao=new BacterinDAO();
		
		this.bacterin=dao.findById(id);
		
		if(bacterin.getTargetBacterin()!=-1){
			Bacterin temp=dao.findById(bacterin.getTargetBacterin());
			bacterin.setTargetBacterinName(temp.getName());
		}else{
			bacterin.setTargetBacterinName("出生日期");				
		}		
		this.sedObj();
		dao.getSession().close();
		return "edit";
	}

	public String doSave(){
		Transaction tx=null;
		try {
			this.buildObj();
			BacterinDAO dao=new BacterinDAO();
			tx=dao.getSession().beginTransaction();
			if(null==this.bacterin.getId()){
//			insert a new object
			//gave the object id by database 
				dao.getSession().clear();
				dao.add(bacterin);
				
				
			}else{
//			update the object
				dao.getSession().clear();
				
				dao.update(bacterin);
			}
			
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
			return "error";
		}
		return "list";
	}

	public String doDetail(){
			BacterinDAO dao=new BacterinDAO();
		this.bacterin=dao.findById(id);
		this.sedObj();;
		if(null!=fromTree){;
			return "detail";
		}else{
			return "list";
		}
	}

	public String doDelete(){
		try{
			BacterinDAO dao=new BacterinDAO();
			dao.deleteObjs(ids);
			return "list";
		}catch(Exception e){
			return "error";
		}
	}

	private void buildObj(){
		this.bacterin=new Bacterin();
		this.bacterin.setId(this.id);
		this.bacterin.setName(this.name);
		this.bacterin.setPrice(this.price);
		this.bacterin.setOffNum(this.offNum);
		this.bacterin.setOffNumUnit(this.offNumUnit);
		this.bacterin.setType(this.type);
		this.bacterin.setTargetBacterin(this.targetBacterin);
		this.bacterin.setStatus(new Integer("1"));
		this.bacterin.setTargetBacterinName(this.targetBacterinName);
	}
	private void sedObj(){

		if(null!=this.bacterin){
			this.id=this.bacterin.getId();
			this.name=this.bacterin.getName();
			this.price=this.bacterin.getPrice();
			this.offNum=this.bacterin.getOffNum();
			this.offNumUnit=this.bacterin.getOffNumUnit();
			this.type=this.bacterin.getType();
			this.targetBacterin=this.bacterin.getTargetBacterin();
			this.targetBacterinName=this.bacterin.getTargetBacterinName();
		}
	}
	public java.lang.String getTargetBacterinName() {
		return targetBacterinName;
	}
	public void setTargetBacterinName(java.lang.String targetBacterinName) {
		this.targetBacterinName = targetBacterinName;
	}
}